![]() |
Java Database Programming with JDBC
by Pratik Patel Coriolis, The Coriolis Group ISBN: 1576100561 Pub Date: 10/01/96 |
Previous | Table of Contents | Next |
The Driver class is the entry point for all JDBC drivers. From here, a connection to the database can be made in order to perform work. This class is intentionally very small; the intent is that JDBC drivers can be pre-registered with the system, enabling the DriverManager to select an appropriate driver given only a URL (Universal Resource Locator). The only way to determine which driver can service the given URL is to load the Driver class and let each driver respond via the acceptsURL method. To keep the amount of time required to find an appropriate driver to a minimum, each Driver class should be as small as possible so it can be loaded quickly.
Register Thyself
The very first thing that a driver should do is register itself with the DriverManager. The reason is simple: You need to tell the DriverManager that you exist; otherwise you may not be loaded. The following code illustrates one way of loading a JDBC driver:
java.sql.Driver d = (java.sql.Driver) Class.forName ("jdbc.SimpleText.SimpleTextDriver").newInstance(); Connection con = DriverManager.getConnection("jdbc:SimpleText", "", "");
The class loader will create a new instance of the SimpleText JDBC driver. The application then asks the DriverManager to create a connection using the given URL. If the SimpleText driver does not register itself, the DriverManager will not attempt to load it, which will result in a nasty No capable driver error.
The best place to register a driver is in the Driver constructor:
public SimpleTextDriver() throws SQLException { // Attempt to register this driver with the JDBC DriverManager. // If it fails, an exception will be thrown. DriverManager.registerDriver(this); }
URL Processing
As I mentioned a moment ago, the acceptsURL method informs the DriverManager whether a given URL is supported by the driver. The general format for a JDBC URL is
jdbc:subprotocol:subname
where subprotocol is the particular database connectivity mechanism supported (note that this mechanism may be supported by multiple drivers) and the subname is defined by the JDBC driver. For example, the format for the JDBC-ODBC Bridge URL is:
jdbc:odbc:data source name
Thus, if an application requests a JDBC driver to service the URL of
jdbc:odbc:foobar
the only driver that will respond that the URL is supported is the JDBC-ODBC Bridge; all others will ignore the request.
Listing 10.14 shows the acceptsURL method for the SimpleText driver. The SimpleText driver will accept the following URL syntax:
jdbc:SimpleText
Note that no subname is required; if a subname is provided, it will be ignored.
Listing 10.14 The acceptsURL method.
//------------------------------------------------------------------------ // acceptsURL - JDBC API // // Returns true if the driver thinks that it can open a connection // to the given URL. Typically, drivers will return true if they // understand the subprotocol specified in the URL, and false if // they don't. // // url The URL of the database. // // Returns true if this driver can connect to the given URL. //------------------------------------------------------------------------ public boolean acceptsURL( String url) throws SQLException { if (traceOn()) { trace("@acceptsURL (url=" + url + ")"); } boolean rc = false; // Get the subname from the url. If the url is not valid for // this driver, a null will be returned. if (getSubname(url) != null) { rc = true; } if (traceOn()) { trace(" " + rc); } return rc; } //------------------------------------------------------------------------ // getSubname // Given a URL, return the subname. Returns null if the protocol is // not "jdbc" or the subprotocol is not "simpletext." //------------------------------------------------------------------------ public String getSubname( String url) { String subname = null; String protocol = "JDBC"; String subProtocol = "SIMPLETEXT"; // Convert to uppercase and trim all leading and trailing // blanks. url = (url.toUpperCase()).trim(); // Make sure the protocol is jdbc: if (url.startsWith(protocol)) { // Strip off the protocol url = url.substring (protocol.length()); // Look for the colon if (url.startsWith(":")) { url = url.substring(1); // Check the subprotocol if (url.startsWith(subProtocol)) { // Strip off the subprotocol, leaving the subname url = url.substring(subProtocol.length()); // Look for the colon that separates the subname // from the subprotocol (or the fact that there // is no subprotocol at all). if (url.startsWith(":")) { subname = url.substring(subProtocol.length()); } else if (url.length() == 0) { subname = ""; } } } } return subname; }
Driver Properties
Connecting to a JDBC driver with only a URL specification is great, but the vast majority of the time, a driver will require additional information in order to properly connect to a database. The JDBC specification has addressed this issue with the getPropertyInfo method. Once a Driver has been instantiated, an application can use this method to find out what required and optional properties can be used to connect to the database. You may be tempted to require the application to embed properties within the URL subname, but by returning them from the getPropertyInfo method, you can identify the properties at runtime, giving a much more robust solution. Listing 10.15 shows an application that loads the SimpleText driver and gets the property information.
Previous | Table of Contents | Next |